package com.apanal.qlife.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.dictionary.DictionaryMap;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.common.util.IdWorkerFactory;
import com.apanal.qlife.sys.dao.DictionaryMapper;
import com.apanal.qlife.sys.model.Dictionary;
import com.apanal.qlife.sys.service.DictionaryService;

/**
 * 数据字典服务层接口实现类
 * 
 * @author shuliangxing
 * 
 * @date 2015-1-14上午11:36:08
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {

	@Autowired
	DictionaryMapper mapper;

	@Override
	public int deleteById(Long id) {
		int result = mapper.deleteByPrimaryKey(id);
		// 刷新字典
		DictionaryMap.reload();
		return result;
	}

	@Override
	public int insert(Dictionary record) {
		if (record.getId() == null) {
			record.setId(IdWorkerFactory.nextId());
		}
		int result = mapper.insert(record);
		// 刷新字典
		DictionaryMap.reload();
		return result;
	}

	@Override
	public int insertSelective(Dictionary record) {
		int result = mapper.insertSelective(record);
		// 刷新字典
		DictionaryMap.reload();
		return result;
	}

	@Override
	public Dictionary selectById(Long id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByIdSelective(Dictionary record) {
		int result = mapper.updateByPrimaryKeySelective(record);
		// 刷新字典
		DictionaryMap.reload();
		return result;
	}

	@Override
	public int updateById(Dictionary record) {
		int result = mapper.updateByPrimaryKey(record);
		// 刷新字典
		DictionaryMap.reload();
		return result;
	}

	@Override
	public List<Dictionary> selectAll() {
		return mapper.select(null);
	}

	@Override
	public PageReturn<Dictionary> selectForPage(Dictionary entity, Page page) {
		int count = mapper.selectForPageCount(entity, page);
		List<Dictionary> list = mapper.selectForPage(entity, page);
		return new PageReturn<Dictionary>(list, count, page, entity);
	}

	@Override
	public int updateToEnableOrDisable(Dictionary dictionary) {
		dictionary
				.setNormal(dictionary.getNormal() == Constants.IS_VALID_TRUE ? Constants.IS_VALID_FALSE
						: Constants.IS_VALID_TRUE);
		int result = mapper.updateByPrimaryKeySelective(dictionary);
		// 刷新字典
		DictionaryMap.reload();
		return result;
	}

	@Override
	public boolean checkUniqueIndex(Long id, int code, String type,
			String language) {
		Boolean result = true;
		Dictionary dictionary = new Dictionary();
		dictionary.setCode(code);
		dictionary.setType(type);
		dictionary.setLanguage(language);
		List<Dictionary> list = mapper.select(dictionary);
		// 用户id大于0,表示修改校验,排除当前记录
		if (id > 0) {
			for (Dictionary d : list) {
				if (!d.getId().equals(id)) {
					result = false;
					break;
				}
			}
		}
		// 表示新增校验
		else {
			result = list.isEmpty();
		}
		return result;
	}

}
